<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="通知公告"></uni-navbar></view>

		<view class="search-view"><u-search class="search-input" @search="search" @custom="search"></u-search></view>

		<u-tabs class="btm-tabs" :list="tabsList" v-model="tabsCurrent" @change="tabsChange" :is-scroll="false"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '' }">
			<view class="list-info_05" v-for="(item, index) of noticeList" :key="item.id" @click="navTo(item)">
				<view class="info-title clamp">
					<span class="left-type" v-if="item.curUserReadStatus == '未读'">未读</span>
					{{ item.title }}
				</view>
				<view class="info-btm">
					{{ item.createTime }}
					<view class="right-btn" v-if="timeDiff(item.attendSignTime) && item.attenderCount > 0">邀您参加</view>
				</view>
			</view>
			<view class="load-more"><u-loadmore :status="loading" /></view>
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import { mapState } from 'vuex';
export default {
	data() {
		return {
			scrollTop: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '全部',
					value: -1
				},
				{
					name: '未读',
					value: 0
				},
				{
					name: '已读',
					value: 1
				}
			],
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b'
			},
			title: '',
			noticeList: [],

			readStatus: '',
			announcements: {
				dataType: '1',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');

				this.pageNum = 1;
				this.pageSize = 15;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				roleCodes: this.roleCodes,
				clientCode: this.clientCode,
				readStatus: this.readStatus,
				announcements: this.announcements
			};
			this.$request('/announcements/myList', data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.noticeList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.noticeList = this.noticeList.concat(res.data.records);
				this.loaded = true;
				if (this.noticeList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.type(this.tabsList[index].value);
		},
		search(e) {
			this.announcements.title = e;
			this.loadData('refresh');
		},
		timeDiff(time) {
			let t1 = new Date(time).getTime();
			let t2 = new Date().getTime();
			return t1 > t2;
		},
		type(e) {
			if (e.toString() == -1) {
				this.readStatus = '';
			} else {
				this.readStatus = e.toString();
			}
			this.loadData('refresh');
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesB/notice2/detail?id=' + item.id
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
	padding: 0 30rpx;

	.load-more {
		margin: 0 -30rpx;
	}
}
</style>
